package org.cyz.tools.core.assembly;

import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import org.controlsfx.control.Notifications;
import org.cyz.tools.Application;
import org.cyz.tools.core.assembly.traynotification.animations.Animations;
import org.cyz.tools.core.assembly.traynotification.models.Location;
import org.cyz.tools.core.assembly.traynotification.notification.Notification;
import org.cyz.tools.core.assembly.traynotification.notification.TrayNotification;
import org.thehecklers.monologfx.MonologFX;
import org.thehecklers.monologfx.MonologFXBuilder;
import org.thehecklers.monologfx.MonologFXButton;
import org.thehecklers.monologfx.MonologFXButtonBuilder;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author abccy
 * @title: ToastUtil
 * @projectName RedisGUI
 * @description: 提示框
 * @date 2020-9-1022:39
 */
public class Notify {


//    String toastMsg = "some text...";
//    int toastMsgTime = 3500; //3.5 seconds
//    int fadeInTime = 50; //0.5 seconds
//    int fadeOutTime= 50; //0.5 seconds
//    Toast.makeText(ContextManager.stageManager.getStage(MainController .class.getSimpleName()), toastMsg, toastMsgTime, fadeInTime, fadeOutTime);

    private static Stage stage = new Stage();
    private static Label label = new Label();

    static {
        stage.initStyle(StageStyle.TRANSPARENT);//舞台透明
        stage.initOwner(Application.stage);
    }

    //默认3秒
    public static void toast(String msg) {
        toast(msg, 3000);
    }

    public static void dialog(String title,String message){
        MonologFXButton mlb = MonologFXButtonBuilder.create()
                .defaultButton(true)
                //.icon("dialog_apply.png")
                .type(MonologFXButton.Type.OK)
                .build();
        MonologFX mono = MonologFXBuilder.create()
                .modal(true)
                .message(message)
                .titleText(title)
                .button(mlb)
                .buttonAlignment(MonologFX.ButtonAlignment.CENTER)
                .build();
        mono.show();
    }

    /**
     * 指定时间消失
     *
     * @param msg
     * @param time
     */
    public static void toast(String msg, int time) {
        label.setText(msg);
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(() -> stage.close());
            }
        };
        init(msg);
        Timer timer = new Timer();
        timer.schedule(task, time);
        stage.show();
    }

    //设置消息
    private static void init(String msg) {
        Label label = new Label(msg);//默认信息
//        label.setStyle("-fx-background: rgba(56,56,56,0.7);-fx-border-radius: 25;-fx-background-radius: 25");//label透明,圆角
        label.setStyle("-fx-background: rgba(56,56,56,0.7);-fx-border-radius: 2;-fx-background-radius: 2");//label透明,圆角
        label.setTextFill(Color.rgb(225, 255, 226));//消息字体颜色
        label.setPrefHeight(50);
        label.setPadding(new Insets(15));
        label.setAlignment(Pos.TOP_CENTER);//居中
        label.setFont(new Font(12));//字体大小
        Scene scene = new Scene(label);
        scene.setFill(null);//场景透明
        stage.setScene(scene);
    }

    @Deprecated
    public static void fxToast(String message, String title, String image) {
        Platform.runLater(() -> Notifications.create()
                .graphic(new ImageView(new Image(image, 64, 64, false, false)))
                .title(title)
                .text(message)
                .position(Pos.CENTER)
                .showInformation());
    }

    /**
     * 基于fx的消息提示
     *
     * @param message
     * @param title
     * @param type
     */
    @Deprecated
    public synchronized static void fxToast(String message, String title, int type) {
        Notifications notifications = Notifications.create().title(title)
                .text(message)
                .position(Pos.CENTER);
        switch (type) {
            case 1:
                notifications.showInformation();
                break;
            case 2:
                notifications.showConfirm();
                break;
            case 3:
                notifications.showWarning();
                break;
            case 4:
                notifications.showError();
                break;
        }
    }

    /**
     * 右上角提示
     *
     * @param message
     * @param title
     * @param type
     */
    @Deprecated
    public static void fxRightToast(String message, String title, int type) {
        Notifications notifications = Notifications.create()
                .title(title)
                .text(message)
                .position(Pos.TOP_RIGHT);
        switch (type) {
            case 1:
                notifications.showInformation();
                break;
            case 2:
                notifications.showConfirm();
                break;
            case 3:
                notifications.showWarning();
                break;
            case 4:
                notifications.showError();
                break;
            default:
                break;
        }
    }


    /**
     * 消息提示框
     * @param title
     * @param message
     * @param type
     */
    public static void notify(String title, String message, int type) {
        Notification notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.SUCCESS;
        switch (type) {
            case 0:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.SUCCESS;
                break;
            case 1:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.INFORMATION;
                break;
            case 2:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.NOTICE;
                break;
            case 3:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.WARNING;
                break;
            case 4:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.ERROR;
                break;
        }
        TrayNotification tray = new TrayNotification();
        tray.setTitle(title);
        tray.setAnimation(Animations.FADE);
        tray.setMessage(message);
        tray.setLocation(Location.TOP_RIGHT);
        tray.setNotification(notification);
        tray.showAndDismiss(new Duration(600));
    }

    /**
     * 　　* @description: 自行控制显示时间的
     * 　　* @param ${tags}
     * 　　* @return ${return_type}
     * 　　* @throws
     * 　　* @author ${USER}
     * 　　* @date 2020年10月7日, 0007 17:05:51
     *
     */
    public static void notifyShowTime(String title, String message, int type, int second) {
        Notification notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.SUCCESS;
        switch (type) {
            case 0:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.SUCCESS;
                break;
            case 1:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.INFORMATION;
                break;
            case 2:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.NOTICE;
                break;
            case 3:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.WARNING;
                break;
            case 4:
                notification = org.cyz.tools.core.assembly.traynotification.notification.Notifications.ERROR;
                break;
        }
        TrayNotification tray = new TrayNotification();
        tray.setTitle(title);
        tray.setAnimation(Animations.FADE);
        tray.setMessage(message);
        tray.setLocation(Location.TOP_RIGHT);
        tray.setNotification(notification);
        tray.showAndDismiss(new Duration(second));
    }
}
